
void munsellCylinder(int u){
  beginShape(QUAD_STRIP);
  for (int i=0; i<=u; i++){
    float angle1 = ((float)i/(float)u)*2*PI;
    float angle2 = ((float)(i+1)/(float)u)*2*PI;

    float x1 = cos(angle1)*radius;
    float x2 = cos(angle2)*radius;
    float z1 = sin(angle1)*radius;
    float z2 = sin(angle2)*radius;

    int hue1 = (i*255)/u;
    int hue2 = ((i+1)*255)/u;

    float y1 = 0;
    float y2 = depth;

    float value1 = 255;
    float value2 = 0;

    fill(hue1,255,value1,96);
    vertex(x1,y1,z1);
    fill(hue1,255,value2,96);
    vertex(x1,y2,z1);
  }
  endShape();
}

void munsellDisc(int u){
  float y = (value*depth)/255;
  beginShape(TRIANGLE_FAN);
  fill(0,0,(255-value));
  vertex(0,y,0);
  for (int i=0; i<=u; i++){
    float angle = ((float)i/(float)u)*2*PI;
    int hueval = (i*255)/u;
    float x11 = cos(angle)*radius;
    float z11 = sin(angle)*radius;
    fill(hueval,255,(255-value));
    vertex(x11,y,z11);
  }
  endShape();
}

void munsellSphere(){
  fill(255,0,255);
  pushMatrix();
  float angle = (mouseX*2*PI) / (float)width;
  float y = (value*depth) / 255.0;
  float rad = (mouseY*radius) / (float) height;
  translate(cos(angle)*rad,y,sin(angle)*rad);
  sphere(5);
  popMatrix();
}

void munsellPalette(){
  // Draw current sphere 
  int h = (mouseX*255) / width;
  int s = (mouseY*255) / height;
  int v = 255 - value;
  fill(h,s,v);
  sphere(10);

  // Draw Palette
  for (int i=0; i<maxsteps; i++){
    pushMatrix();
    translate(50+(25*i),0,0);
    fill(colors[i]);
    sphere(10);
    popMatrix();
  }
}

void munsellBezier(){
  float x1 = positions[0][0];
  float cx1 = positions[1][0];
  float cx2 = positions[2][0];
  float x2 = positions[3][0];
  
  float y1 = positions[0][1];
  float cy1 = positions[1][1];
  float cy2 = positions[2][1];
  float y2 = positions[3][1];
  
  float z1 = positions[0][2];
  float cz1 = positions[1][2];
  float cz2 = positions[2][2];
  float z2 = positions[3][2];
  
  stroke(255);
  //strokeWeight(10);
  noFill();
  bezier(x1, y1, z1, cx1, cy1, cz1, cx2, cy2, cz2, x2, y2, z2);
  noStroke();
}

void munsellClick(){
  steps = steps%maxsteps;
  int h = (mouseX*255) / width;
  int s = (mouseY*255) / height;
  int v = 255 - value;
  colors[steps] = color(h,s,v);
  float angle = (mouseX*2*PI) / (float)width;
  float y = (value*depth) / 255.0;
  float rad = (mouseY*radius) / (float) height;
  positions[steps][0] = cos(angle)*rad;
  positions[steps][1] = y;
  positions[steps][2] = sin(angle)*rad;
  steps ++;
}
